﻿Public Class frmTabAnalyticalTools
    Dim editNode As EditXML = New EditXML()
    Public Shared idTools As Collection = New Collection()
    Public Shared sPdelim As String = "£"

    Private Sub btnToolAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnToolAdd.Click
        Dim sList As String = ""
        Dim aTool As New ToolClass
        Dim frmNewTool As New frmTools
        frmNewTool.ShowDialog()
        If (frmNewTool.getUpdated() = True) Then
            aTool = frmNewTool.getObjTool()
            idTools.Add(aTool)
            sList = aTool.ToolDesc
            lboxTool.Items.Add(sList)
        End If
    End Sub

    Private Sub btnToolEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnToolEdit.Click
        Dim iCount As Integer = lboxTool.Items.Count
        Dim iToEdit As Integer = lboxTool.SelectedIndex
        If (iCount = 1) Then iToEdit = 0
        If (iToEdit = -1) Then
            MessageBox.Show("No tool chosen for editing", "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If

        Dim slist As String = ""
        Dim editTool As ToolClass = CType(idTools(iToEdit + 1), ToolClass)
        Dim frmEditTool As New frmTools(editTool)
        frmEditTool.ShowDialog()
        If (frmEditTool.getUpdated() = True) Then
            editTool = frmEditTool.getObjTool()
            idTools.Add(editTool, , iToEdit + 1) 'one-based index
            idTools.Remove(iToEdit + 2)
            slist = editTool.ToolDesc
            lboxTool.Items.Insert(iToEdit, slist) 'zero-based index
            lboxTool.Items.RemoveAt(iToEdit + 1)
        End If
        btnToolEdit.Enabled = False
        btnToolDelete.Enabled = False
    End Sub

    Private Sub btnToolDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnToolDelete.Click
        Dim iToDelete As Integer = lboxTool.SelectedIndex
        If (iToDelete = -1) Then
            MessageBox.Show("No tool chosen for deletion", "Selection error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If
        lboxTool.Items.RemoveAt(iToDelete) 'zero-based index
        idTools.Remove(iToDelete + 1) 'one-based index
        btnToolEdit.Enabled = False
        btnToolDelete.Enabled = False
    End Sub

    Private Sub lboxTool_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lboxTool.SelectedIndexChanged
        If lboxTool.SelectedItems.Count() > 0 Then
            btnToolEdit.Enabled = True
            btnToolDelete.Enabled = True
        Else
            btnToolEdit.Enabled = False
            btnToolDelete.Enabled = False
        End If
    End Sub

    Private Sub frmTabAnalyticalTools_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Leave
        editXMLFile()
    End Sub
    Public Sub editXMLFile()
        editNode.Remove("/metadata/idinfo", "tool")
        For i As Integer = idTools.Count() To 1 Step -1
            Dim item As ToolClass = idTools(i)
            Dim newNode As String = ""
            newNode = newNode & "<tooldesc>" & item.ToolDesc & "</tooldesc>"
            newNode = newNode & "<toolacc><onlink>" & item.ToolLink & "</onlink><toolinst>" & item.ToolAccess & "</toolinst><toolcomp>" & item.ToolOS & "</toolcomp></toolacc>"
            If item.boolContact() Then
                newNode = newNode & "<toolcont>"
                newNode = newNode & editNode.createContactInfo(item.ToolContact)
                newNode = newNode & "</toolcont>"
            End If

            If item.boolCitation() Then
                newNode = newNode & "<toolcite>"
                newNode = newNode & editNode.createCiteInfo(item.ToolCite(), item.boolCiteLWC(), item.ToolCiteLWC())
                newNode = newNode & "</toolcite>"
            End If
            editNode.Add("/metadata/idinfo", "/secinfo", "tool", newNode)
        Next
    End Sub

    Private Sub button_example_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button_example.Click
        Dim helpForm As dialog_help = New dialog_help("AnalyticalTools")
        helpForm.ShowDialog()
    End Sub
End Class